package com.slg.android.manyanime;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RadioButton;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.slg.android.manyanime.adapter.CollectionAdapter;
import com.slg.android.manyanime.bean.Collection;
import com.slg.android.manyanime.utils.DataStore;

import java.util.LinkedList;
import java.util.List;


public class MyCollectionActivity extends Activity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    private RadioButton rb1;
    private RadioButton rb2;
    private View view1;
    private View view2;
    private DbUtils db;
    private ListView listView;
    private List<Collection> list;
    private CollectionAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_comment_picture_details);

//        //创建数剧库
        db = DataStore.getDataStore().getDb();

        rb1 = (RadioButton) findViewById(R.id.my_collection_activity_rb1);
        rb1.setOnCheckedChangeListener(this);

        rb2 = (RadioButton) findViewById(R.id.my_collection_activity_rb2);
        rb2.setOnCheckedChangeListener(this);

        view1 = findViewById(R.id.my_collection_activity_view1);
        view2 = findViewById(R.id.my_collection_activity_view2);

        View view = findViewById(R.id.my_comment_list_return_img);
        view.setOnClickListener(this);
        listView = (ListView) findViewById(R.id.my_collect_favorite_list);
        list = new LinkedList<Collection>();

        try {

            list = db.findAll(Selector.from(Collection.class).where("type", "=", "video"));
            if (list != null) {
                adapter = new CollectionAdapter(this, list);
                listView.setAdapter(adapter);
            }
//            adapter.notifyDataSetChanged();
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.my_comment_list_return_img:
                finish();
                break;
        }
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int id = buttonView.getId();
        switch (id) {   //
            case R.id.my_collection_activity_rb1:
                if (buttonView.isChecked()) {
                    view2.setBackgroundResource(R.color.common_text_title_color);
                    view1.setBackgroundResource(R.color.main_menu_text_selected);
                }
                //如果点击动画，进行动画收藏的查找
                try {
                    if (list != null) {
                        list.clear();
                        List<Collection> list1 = db.findAll(Selector.from(Collection.class)
                                .where("type", "=", "book"));
                        if (list1.size() > 0 && list1 != null) {
                            list.addAll(list1);
                            adapter.notifyDataSetChanged();
                        }
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }

                break;
            case R.id.my_collection_activity_rb2:
                if (buttonView.isChecked()) {
                    view1.setBackgroundResource(R.color.common_text_title_color);
                    view2.setBackgroundResource(R.color.main_menu_text_selected);
                }
                //如果点击漫画，进行慢画收藏的查找
                try {
                    if (list != null) {
                        list.clear();

                        List<Collection> list1 = db.findAll(Selector.from(Collection.class)
                                .where("type", "=", "video"));
                        if (list1.size() > 0 && list1 != null) {
                            list.addAll(list1);

//                    Log.d("MyCollectionActivity","----->"+list.size());
                            adapter.notifyDataSetChanged();
                        }
                    }

                } catch (DbException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

}
